<?php
namespace Home\Controller;
use Think\Controller;
class WeixinController extends Controller {
    public function index(){
        $redirect_uri=urlencode("http://".$_SERVER['SERVER_NAME'].U('home/weixin/callback'));
        var_dump($redirect_uri);
        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        redirect($url);
    }
    public function callback(){
        var_dump($_GET);
        $code=$_GET['code'];
        $login_access_token_api='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.APPSECRET.'&code='.$code.'&grant_type=authorization_code';
        $login_access_token_data=json_decode(http_get($login_access_token_api));
        $login_access_token=$login_access_token_data->access_token;
        $openid=$login_access_token_data->openid;
        $user_info_api='https://api.weixin.qq.com/sns/userinfo?access_token='.$login_access_token.'&openid='.$openid.'&lang=zh_CN';
        $userinfo=json_decode(http_get($user_info_api));
        if(!(M('user')->where(array('openid'=>$openid))->find())){
            M('user')->add(array(
                'img'=>$userinfo->headimgurl,
                'nickname'=>$userinfo->nickname,
                'sex'=>$userinfo->sex,
                'posotion'=>$userinfo->country.$userinfo->province.$userinfo->city,
                'openid'=>$openid,
            ));
        }
        $this->assign('userinfo',(array)$userinfo);
        $this->display();
    }
    public function groupSend(){
        $url="https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=".getAccessToken();
        $openids=M('user')->field('openid')->select();
        $openid=array();
        foreach ($openids as $v) {
            $openid[]=$v['openid'];
        }
        $array=array(
            'touser'=>$openid,
            'msgtype'=>'text',
            'text'=>array('content'=>"上午好"),
        );
        $postJson=json_encode($array,JSON_UNESCAPED_UNICODE);
        $res=http_post($url,$postJson);
        var_dump($res);
    }
    /*public function imagetext(){
        $url="https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=".getAccessToken();
        $data={
           "articles":[
                {
                    "thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p",
                    "author":"xxx",
                    "title":"Happy Day",
                    "content_source_url":"www.qq.com",
                    "content":"content",
                    "digest":"digest",
                    "show_cover_pic":1
                },
            ]
        }
    }*/
    public function templateMessage(){
        $url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".getAccessToken();
        $data='{
            "touser":"ovOCCwaLIJrMbJKdwNpjOB-pdgHY",
            "template_id":"aHp6prWu1-ouTRr6dFLY0u75R4QuiDLAnGDhDwbr3A0",
            "url":"http://weixin.qq.com/download",            
            "data":{
                "first": {
                    "value":"恭喜你购买成功！",
                    "color":"#173177"
                },
                "keyword1":{
                    "value":"巧克力",
                    "color":"#173177"
                },
                "keyword2": {
                    "value":"39.8元",
                    "color":"#173177"
                },
                "keyword3": {
                    "value":"2014年9月22日",
                    "color":"#173177"
                },
                "keyword4": {
                    "value":"2014年9月23日",
                    "color":"#173177"
                },
                "remark":{
                    "value":"欢迎再次购买！",
                    "color":"#173177"
                }
            }
        }';
        $res=http_post($url,$data);
        var_dump($res);
    }
}